import 'package:flutter/material.dart';

class CustomDialog extends Dialog {
  final String? title;
  late final String? content;
  final Widget? contentWidget;
  final String? confirm;
  final String? cancel;
  final Color? confirmColor;
  final Color? cancelColor;
  final Color? confirmBgColor;
  final Color? cancelBgColor;
  final Color? confirmBorderColor;
  final Color? cancelBorderColor;
  final Function()? confirmBlock;
  final Function()? cancelBlock;
  CustomDialog({
    Key? key,
    this.title,
    this.content,
    this.contentWidget,
    this.confirm,
    this.cancel,
    this.confirmColor,
    this.cancelColor,
    this.confirmBgColor,
    this.cancelBgColor,
    this.confirmBorderColor,
    this.cancelBorderColor,
    this.confirmBlock,
    this.cancelBlock,
  }) : super(key: key){
    if(contentWidget != null) content = null;
  }
  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.transparency,
      child: Center(
        child: Container(
          constraints: BoxConstraints(maxHeight: 300),
          width: double.infinity,
          margin: const EdgeInsets.all(30),
          decoration: const ShapeDecoration(
              color: Colors.white,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(5.0)))
          ),

          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                height: 40,
                decoration: BoxDecoration(
                  color: Color(0xFFF7F8FB),
                  borderRadius: BorderRadius.vertical(top: Radius.circular(5))
                ),
                child: Center(
                  child: Text(title ?? '',style: TextStyle(
                    fontSize: 14,
                    color: Color(0xFF9EA5B6),
                  ),),
                ),
              ),

              Offstage(
                  offstage: contentWidget == null,
                  child: contentWidget,
              ),

              Offstage(
                offstage: content == null,
                child: Container(
                  margin: EdgeInsets.fromLTRB(0, 17, 0, 17),
                  child: Text(content??"",
                    style: TextStyle(
                      fontSize: 15,
                      color: Color(0xFF595959)
                    ),
                    textAlign: TextAlign.center,
                  ),
                )
              ),

             if(cancel != null || confirm != null) getBottomWidget(context),
            ],
          ),
        ),
      ),
    );
  }

  getBottomWidget(BuildContext context){
    return Container(
      height: 32,
      margin: EdgeInsets.fromLTRB(0, 0, 0, 20),
      child: Row(
        children: [
          SizedBox(width: 15,),
          if(cancel != null) Expanded(
              child: customButton(
                  title: cancel,
                  titleColor: cancelColor??const Color(0xFF5C5A64),
                  bgColor: cancelBgColor??const Color(0xFFFFFFFF),
                  borderColor: cancelBorderColor??const Color(0xFF6A7081),
                  callBack: (){
                    Navigator.pop(context);
                    if(cancelBlock != null) cancelBlock!();
                  }
              ),
          ),

          Offstage(
            offstage: cancel == null || confirm == null,
            child: SizedBox(width: 20,),
          ),
          if(confirm != null) Expanded(
            child: customButton(
              title: confirm,
              titleColor: confirmColor??const Color(0xFFFFFFFF),
              bgColor: confirmBgColor??const Color(0xFFFB1B1B),
              borderColor: confirmBorderColor??const Color(0xFFFB1B1B),
              callBack: (){
                Navigator.pop(context);
                if(confirmBlock != null) confirmBlock!();
              }
            ),
          ),
          SizedBox(width: 15,),

        ],
      ),
    );
  }

  customButton({
    String? title,
    Color titleColor = const Color(0xFF5C5A64),
    Color bgColor = const Color(0xFFFFFFFF),
    Color borderColor = const Color(0xFF6A7081),
     Function()? callBack,
  })  {
    return InkWell(
      onTap: callBack ,
      child: Container(
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: bgColor,
          borderRadius: BorderRadius.all(Radius.circular(4)),
          border: Border.all(
            color: borderColor,
            width: 1,
          ),
        ),
        child: Text(
          title??"",
          style: TextStyle(
            fontSize: 14,
            color: titleColor,
          ),
        ),
      ),
    );
  }
}
